package com.pengxun.manager.controller.system;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pengxun.manager.model.system.MenuApi;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.MenuApiService;
import com.pengxun.manager.vo.MenuApiVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/menuApi")
public class  MenuApiController {
	
	private static final Logger log = LoggerFactory.getLogger(MenuApiController.class);

   @Resource
   private MenuApiService menuApiService;
   @Resource 
   LoginService loginService;
   
   @RequestMapping("/insert")
   public Result insert(@Valid @RequestBody MenuApi menuApi,BindingResult result){
       try{
    	   if(result.hasErrors()) {
    		   throw new EastrocException(result.getFieldError().getDefaultMessage());
    	   }
    	   Date date = new Date();
    	   menuApi.setCreateDate(date);
//    	   menuApi.setCreateBy(loginService.getCurrentUser().getUserId());
    	   menuApi.setUpdateDate(date);
//    	   menuApi.setUpdateBy(loginService.getCurrentUser().getUserId());
    	   menuApiService.insert(menuApi);
       } catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   };
   
   @RequestMapping("/update")
   public Result update(@RequestBody MenuApi menuApi){
        try{
        	Date date = new Date();
        	menuApi.setUpdateDate(date);
        	menuApi.setUpdateBy(loginService.getCurrentUser().getUserId());
        	menuApiService.update(menuApi);
       } catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   };
   
   @RequestMapping("/delete")
   public Result delete(Integer id){
       try{
          menuApiService.delete(id);
       } catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   };
   
   @RequestMapping("/findByPage")
   public Result findByPage(@RequestBody MenuApiVO menuApiVO) {
		Page page = menuApiService.findByPage(menuApiVO);
		return ResultUtils.render(page);
	};
	
   @RequestMapping("/findById")
   public Result findById(Integer id) {
		MenuApi menuApi = menuApiService.findById(id);
		return ResultUtils.render(menuApi);
	};
	@RequestMapping("/findByMenuCode")
	public Result findByMenuCode(String menuCode) {
		log.info("menuCode=={}",menuCode);
		List<MenuApi> menuApis = menuApiService.findByMenuCode(menuCode);
		return ResultUtils.render(menuApis);
	};
	
	@RequestMapping("/updateStatus")
   public Result updateStatus(Integer id,Integer status){
	   try{
		   menuApiService.updateStatus(id,status);
	   } catch (Exception e) {
		   return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   }
}
